﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Taobao.Models;

namespace Taobao.Util
{
    /// <summary>
    /// HTML 解析规则
    /// </summary>
    public class Rule
    {
        HtmlDocument doc;// = new HtmlDocument();
        private string _html = "";
        private string _taoShopUrl = "";//店铺在淘宝的地址
        public Rule(string html, string taoShopUrl)
        {
            _html = html;
            _taoShopUrl = taoShopUrl;
            doc = new HtmlDocument();
            doc.LoadHtml(html);
        }
        public List<auctionsInshop> GetProductList()
        {
            string shopurl = "";
            var fistCon = doc.DocumentNode.SelectNodes("//span[@class='shop-name']");
            if (fistCon != null && fistCon.Count > 0)
            {
                //获取店铺地址 店内搜索 地址
                var shopurlList = GetAttr(fistCon[0].InnerHtml, "a[@href]", "href");
                if (shopurlList != null && shopurlList.Count > 0)
                {
                    shopurl = shopurlList[0];
                }
            }
            doc.LoadHtml(_html);
            var collection = doc.DocumentNode.SelectNodes("//dl[@class='item   ']");
            if (collection != null && collection.Count > 0)
            {
                return GetProductRule1(collection, shopurl);
            }
            else
            {

                return null;
            }

        }
        /// <summary>
        /// 规则1 抓取商品信息
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        public List<auctionsInshop> GetProductRule1(HtmlNodeCollection collection, string shopurl)
        {
            List<auctionsInshop> list = new List<auctionsInshop>();
            List<string> htmList = new List<string>();
            if (collection == null || collection.Count == 0)
            {
                return list;
            }
            for (int k = 0; k < collection.Count; k++)
            {
                htmList.Add(collection[k].InnerHtml);
            }
            collection = doc.DocumentNode.SelectNodes("//dl[@class='item  last ']");
            for (int k = 0; k < collection.Count; k++)
            {
                htmList.Add(collection[k].InnerHtml);
            }
            for (int k = 0; k < htmList.Count; k++)
            {
                var dl = htmList[k];
                auctionsInshop asi = new auctionsInshop();
                if (dl == null)
                {
                    continue;
                }
                var itemlist = GetAttr(dl, "//dt/a[@href]", "href");
                if (itemlist.Count == 0)
                {
                    continue;
                }
                asi.url = "https:" + itemlist[0];//商品链接
                itemlist = GetAttr(dl, "//dt/a/img[@data-ks-lazyload]", "data-ks-lazyload");
                if (itemlist.Count == 0)
                {
                    continue;
                }
                asi.picUrl = "https:" + itemlist[0]; //图片链接
                itemlist = GetAttr(dl, "//dt/a/img[@data-ks-lazyload]", "alt");
                if (itemlist.Count == 0)
                {
                    continue;
                }
                asi.title = itemlist[0]; //名称
                //获取价格
                doc.LoadHtml(dl);
                var priccoll = doc.DocumentNode.SelectNodes("//span[@class='c-price']");
                asi.price = priccoll[0].InnerText;
                asi.searchUrl = shopurl;
                //查找已买数量 评论数和收藏数
                //var html = SpiderMethod.GetHtmlByHttpHelper(asi.url);
                //doc.LoadHtml(html);
                //collection = doc.DocumentNode.SelectNodes("//strong[@id='J_RateCounter']");
                asi.shopurl = _taoShopUrl;
                list.Add(asi);
            }
            return list;
        }
        /// <summary>
        ///根据规则获取属性值
        /// </summary>
        /// <param name="htmlContent"></param>
        /// <returns></returns>
        public List<string> GetAttr(string htmlContent, string rule, string attr)
        {
            List<string> list = new List<string>();
            doc.LoadHtml(htmlContent);
            var conllectin = doc.DocumentNode.SelectNodes(rule);//".//a[@href]"
            if (conllectin != null)
            {
                for (int index = 0; index < conllectin.Count; index++)
                {
                    HtmlAttribute att = conllectin[index].Attributes[attr];
                    list.Add(att.Value);
                }
            }
            return list;
        }
    }
}